<?php

namespace front\modules\web\controllers;

use Yii;
use front\models\RegionModel;
use yii\data\ActiveDataProvider;
use front\controllers\WebBaseController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use users\services\RegionService;
use yii\helpers\ArrayHelper;

/**
 * RegionController implements the CRUD actions for RegionModel model.
 */
class RegionController extends WebBaseController
{
    private $rid;
    public $enableCsrfValidation = false;
    function init(){
        parent::init();
        $this->rid = intval(Yii::$app->request->get('id'));
        if(!$this->rid){
            WebBaseController::getAjaxData(2,'城市或地区不存在');
        }

    }
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     *获取下级行政区域信息
     * @return mixed
     */
    public function actionIndex()
    {
       $info = RegionService::getChild($this->rid);
       $arr = ArrayHelper::toArray($info['result']);
        if($info['code']==0){
            WebBaseController::getAjaxData(1,'获取成功',$arr);
        }

    }

    /**
     * 获取完整的行政区域信息
     */
    function actionGetparent(){
        $info = RegionService::getParent($this->rid);
        $arr = ArrayHelper::toArray($info['result']);
        if($info['code']==0){
            WebBaseController::getAjaxData(1,'获取成功',$arr);
        }
    }




    /**
     * Displays a single RegionModel model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new RegionModel model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new RegionModel();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing RegionModel model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing RegionModel model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the RegionModel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return RegionModel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = RegionModel::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
